﻿-- =============================================
-- Script Template
-- =============================================
DROP SYMMETRIC KEY [SYM_KEY]
GO
CREATE SYMMETRIC KEY [SYM_KEY]
    AUTHORIZATION [dbo]
    WITH ALGORITHM = TRIPLE_DES
    ENCRYPTION BY PASSWORD = N'##SACM12##';

DECLARE @KEYID UNIQUEIDENTIFIER
DECLARE @sPass NVARCHAR(2000), @sPassword NVARCHAR(200)
SET @KEYID = KEY_GUID('SYM_KEY')

OPEN SYMMETRIC KEY SYM_KEY
DECRYPTION BY PASSWORD='##SACM12##'

SET @sPassword = 'password'
SELECT @sPass = ENCRYPTBYKEY(@KEYID,@sPassword)
SELECT @sPass
        
--SELECT * FROM security.CatUsuario

----------------------------------------------
-- security.CatEstatusUsuario
----------------------------------------------
IF NOT EXISTS( SELECT 1 FROM security.CatEstatusUsuario WHERE IdEstatusUsuario = 1 ) INSERT INTO security.CatEstatusUsuario(IdEstatusUsuario,Descripcion) VALUES(1,'Activo')
IF NOT EXISTS( SELECT 1 FROM security.CatEstatusUsuario WHERE IdEstatusUsuario = 2 ) INSERT INTO security.CatEstatusUsuario(IdEstatusUsuario,Descripcion) VALUES(2,'Bloqueado')
IF NOT EXISTS( SELECT 1 FROM security.CatEstatusUsuario WHERE IdEstatusUsuario = 3 ) INSERT INTO security.CatEstatusUsuario(IdEstatusUsuario,Descripcion) VALUES(3,'Inactivo')

INSERT INTO security.CatUsuario
        ( IdUsuario ,
          IdEmpresa ,
          IdSucursal ,
          IdEstatus ,
          UserName ,
          Password
        )
VALUES  ( 1 , -- IdUsuario - int
          1 , -- IdEmpresa - int
          1 , -- IdSucursal - int
          1 , -- IdEstatus - int
          N'adminsacm' , -- UserName - nvarchar(20)
          @sPass  -- Password - nvarchar(200)
        )

----------------------------------------------
-- security.CatEmpresa
----------------------------------------------
IF NOT EXISTS( SELECT 1 FROM security.CatEmpresa WHERE IdEmpresa = 1 ) INSERT INTO security.CatEmpresa(IdEmpresa,Nombre,IdEstado,IdMunicipio,CodigoPostal,IdColonia,Calle,NumeroExt,NumeroInt,Telefono,FechaMod,IpMod,IdUsuarioMod,LogoArchivo,LogoNombre,LogoImageType,Tema,Titular) VALUES(1,'Demo',1,1,0,0,NULL,0,0,NULL,GETDATE(),'',0,NULL,NULL,NULL,'Default',NULL)

----------------------------------------------
-- security.CatSucursal
----------------------------------------------
IF NOT EXISTS( SELECT 1 FROM security.CatSucursal WHERE IdSucursal = 1 AND IdEmpresa = 1 ) INSERT INTO security.CatSucursal(IdSucursal,IdEmpresa,Nombre,IdEstado,IdMunicipio,CodigoPostal,Calle,IdColonia,NumeroExt,NumeroInt,Telefono,FechaMod,IpMod,IdUsuarioMod) VALUES(1,1,'Sucursal 1',1,1,0,NULL,0,0,0,NULL,GETDATE(),'',0)


GRANT EXECUTE ON [security].spObtenPerfilModulo TO [security]
GRANT EXECUTE ON [security].spObtenPermisoItem TO [security]
GRANT EXECUTE ON [security].spValidaUsuario TO [security]

GRANT ALTER ON SYMMETRIC KEY::SYM_KEY TO security;